<?php
/**
 * 客户后台管理公司信息
 * Author: lyun
 * Date: 2019/3/11
 * Created by Panxsoft.
 */

namespace App\Http\Controllers\Admin\Customer;

use App\Http\Controllers\ApiController;
use App\Http\Requests\Admin\Customer\SunHungKai\CompanyManageStoreRequest;
use App\Http\Requests\Admin\Customer\SunHungKai\CompanyManageUpdateRequest;
use App\Models\Admin\CustomerAdmin;
use App\Models\Admin\CustomerCompany;
use App\Models\Customer\Customer;
use App\Models\Panx\CategoryPivotCustomer;
use App\Services\Utils\Guard;
use Illuminate\Http\Request;

class CompanyManageController extends ApiController
{
    const ROUTES = [
        'index'   => 'customer.company.manage.index',
        'show'    => 'customer.company.manage.show',
        'create'  => 'customer.company.manage.create',
        'store'   => 'customer.company.manage.store',
        'update'  => 'customer.company.manage.update',
        'destroy' => 'customer.company.manage.destroy',
    ];

    /**
     * @test
     * Display a listing of the resource.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function index(Request $request)
    {
        $customer_id = $this->checkCustomerId();

        $customer_companies = (new CustomerCompany)
            ->where('customer_id', $customer_id)
            ->latest()
            ->paginate($request->per_page ?? 10);

        return $this->response(compact('customer_companies'));
    }

    /**
     * 返回试妆产品分类列表
     *
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function create()
    {
        $this->checkCustomerId();
        $customer = Guard::customer();

        $categories = $customer->categories()
            ->filterCustomerPivotIsShow(CategoryPivotCustomer::IS_SHOW)
            ->orderBy((new CategoryPivotCustomer)->qualifyColumn('priority'), 'desc')
            ->get();

        return $this->response(compact('categories'));
    }

    /**
     * @test
     * Store a newly created resource in storage.
     *
     * @param  CompanyManageStoreRequest  $request
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function store(CompanyManageStoreRequest $request)
    {
        $customer_id = $this->checkCustomerId();

        $data = $this->assembleData($request, $customer_id);

        $stored = CustomerCompany::create($data);

        return $this->response(compact('stored'));
    }

    /**
     * @test
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function show($id)
    {
        $customer_id = $this->checkCustomerId();

        $customer_company = CustomerCompany::where('customer_id', $customer_id)->find($id);

        return $this->response(compact('customer_company'));
    }

    /**
     * @test
     * Update the specified resource in storage.
     *
     * @param  CompanyManageUpdateRequest  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function update(CompanyManageUpdateRequest $request, $id)
    {
        $customer_id = $this->checkCustomerId();

        $customer_company = CustomerCompany::where('customer_id', $customer_id)->find($id);
            $this->checkItemExist($customer_company, trans('admin.customer_company'));

        $data = $this->assembleData($request, $customer_id);

        $updated = $customer_company->update($data);

        return $this->response(compact('updated'));
    }

    /**
     * @test
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function destroy($id)
    {
        $customer_id = $this->checkCustomerId();

        /** @var CustomerCompany $customer_company */
        $customer_company = CustomerCompany::where('customer_id', $customer_id)->find($id);
            $this->checkItemExist($customer_company, trans('admin.customer_company'));

        $deleted = $customer_company->delete();

        return $this->response(compact('deleted'));
    }

    /**
     * @param CompanyManageUpdateRequest|CompanyManageStoreRequest|Request $request
     * @param $customer_id
     * @return array
     */
    private function assembleData(Request $request, $customer_id)
    {
        $data = [
            'customer_id'  => $customer_id,
            'company_name' => $request->company_name,
            'category_id'  => $request->category_id,
            'description'  => $request->description,
            'product_price' => $request->product_price,
        ];

//        if (is_file($request->logo)) {
//            $data['logo'] = $request->logo;
//        }
        if (is_file($request->product_image)) {
            $data['product_image'] = $request->product_image;
        }

        return $data;
    }

    /**
     * @return int
     * @throws \Exception
     */
    private function checkCustomerId()
    {
        // 超管才能操作的
        $this->checkIsTrue(CustomerAdmin::isGuardSuperAdmin(), trans('admin.is_super_man'));

        /** @var Customer $customer */
        $customer = Guard::customer();
            $this->checkIsTrue(($customer->isSunHungKai()), trans('admin.is_specify_customer_id'));

        return $customer->id;
    }
}
